package com.zycfc.zsf.boot.core.zsfvalue;

import org.springframework.beans.factory.config.*;
import org.springframework.context.annotation.*;
import org.springframework.core.env.*;
import org.springframework.beans.factory.annotation.*;
import java.lang.reflect.*;
import org.springframework.beans.*;
import com.zycfc.zsf.boot.util.text.*;
import java.util.*;
import org.springframework.util.*;
import org.slf4j.*;

@Configuration
public class ZsfValueProcessor implements BeanPostProcessor
{
    private static final Logger log;
    private final PlaceholderHelper placeholderHelper;
    @Autowired
    Environment env;
    
    public ZsfValueProcessor() {
        this.placeholderHelper = new PlaceholderHelper();
    }
    
    public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
        final Class<?> obj = bean.getClass();
        final List<Field> fields = this.findAllField(obj);
        final ZsfValue zsfValue;
        ZsfKeys keys;
        final Iterator<String> iterator;
        String key;
        fields.stream().filter(field -> field.getAnnotation(ZsfValue.class) != null).forEach(field -> {
            zsfValue = field.getAnnotation(ZsfValue.class);
            if (zsfValue != null) {
                keys = this.placeholderHelper.extractPlaceholderKeys(zsfValue.value());
                keys.getKeys().iterator();
                while (iterator.hasNext()) {
                    key = iterator.next();
                    this.setField(zsfValue.value(), zsfValue.signType(), key, keys.getDefaultValue(), bean, beanName, field);
                }
            }
            return;
        });
        return bean;
    }
    
    private void setField(final String pvalue, final ZsfValSignType signType, final String key, final String defaultValue, final Object bean, final String beanName, final Field field) {
        final ZsfValueOperator springValue = new ZsfValueOperator(key, pvalue, bean, beanName, field, false, defaultValue);
        ZsfValueCacheMap.map.put((Object)key, (Object)springValue);
        try {
            String value = null;
            if (this.env.containsProperty(key)) {
                final String valueStr = this.env.getProperty(key);
                if (!ZsfValSignType.NO.equals(signType)) {
                    throw new RuntimeException("\u6682\u4e0d\u652f\u6301[" + signType + "]\u52a0\u5bc6\u65b9\u5f0f");
                }
                value = valueStr;
            }
            else {
                value = defaultValue;
            }
            if (!StringUtils.isEmpty(value)) {
                springValue.update(value);
            }
        }
        catch (Exception e) {
            ZsfValueProcessor.log.error("\u6ce8\u89e3\u89e3\u6790\u5931\u8d25", (Throwable)e);
        }
    }
    
    private List<Field> findAllField(final Class<?> clazz) {
        final List<Field> res = new LinkedList<Field>();
        ReflectionUtils.doWithFields((Class)clazz, (ReflectionUtils.FieldCallback)new ReflectionUtils.FieldCallback() {
            public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
                res.add(field);
            }
        });
        return res;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZsfValueProcessor.class);
    }
}
